package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.TDaAlarmDataTempBo;
import org.dromara.system.domain.vo.TDaAlarmDataTempVo;
import org.dromara.system.domain.TDaAlarmDataTemp;
import org.dromara.system.mapper.TDaAlarmDataTempMapper;
import org.dromara.system.service.ITDaAlarmDataTempService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 报警信息表Service业务层处理
 *
 * @author Liu
 * @date 2023-11-23
 */
@RequiredArgsConstructor
@Service
public class TDaAlarmDataTempServiceImpl implements ITDaAlarmDataTempService {

    private final TDaAlarmDataTempMapper baseMapper;

    /**
     * 查询报警信息表
     */
    @Override
    public TDaAlarmDataTempVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询报警信息表列表
     */
    @Override
    public TableDataInfo<TDaAlarmDataTempVo> queryPageList(TDaAlarmDataTempBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TDaAlarmDataTemp> lqw = buildQueryWrapper(bo);
        Page<TDaAlarmDataTempVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询报警信息表列表
     */
    @Override
    public List<TDaAlarmDataTempVo> queryList(TDaAlarmDataTempBo bo) {
        LambdaQueryWrapper<TDaAlarmDataTemp> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TDaAlarmDataTemp> buildQueryWrapper(TDaAlarmDataTempBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TDaAlarmDataTemp> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEquipCode()), TDaAlarmDataTemp::getEquipCode, bo.getEquipCode());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmCode()), TDaAlarmDataTemp::getAlarmCode, bo.getAlarmCode());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmText()), TDaAlarmDataTemp::getAlarmText, bo.getAlarmText());
        lqw.eq(bo.getAlarmTime() != null, TDaAlarmDataTemp::getAlarmTime, bo.getAlarmTime());
        lqw.eq(bo.getClearTime() != null, TDaAlarmDataTemp::getClearTime, bo.getClearTime());
        lqw.eq(bo.getActive() != null, TDaAlarmDataTemp::getActive, bo.getActive());
        lqw.eq(bo.getUpdateTime() != null, TDaAlarmDataTemp::getUpdateTime, bo.getUpdateTime());
        lqw.eq(bo.getDataStatus() != null, TDaAlarmDataTemp::getDataStatus, bo.getDataStatus());
        lqw.eq(bo.getLastArchiveTime() != null, TDaAlarmDataTemp::getLastArchiveTime, bo.getLastArchiveTime());
        lqw.eq(StringUtils.isNotBlank(bo.getPlantNO()), TDaAlarmDataTemp::getPlantNO, bo.getPlantNO());
        lqw.eq(bo.getDataFlag() != null, TDaAlarmDataTemp::getDataFlag, bo.getDataFlag());
        return lqw;
    }

    /**
     * 新增报警信息表
     */
    @Override
    public Boolean insertByBo(TDaAlarmDataTempBo bo) {
        TDaAlarmDataTemp add = MapstructUtils.convert(bo, TDaAlarmDataTemp.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改报警信息表
     */
    @Override
    public Boolean updateByBo(TDaAlarmDataTempBo bo) {
        TDaAlarmDataTemp update = MapstructUtils.convert(bo, TDaAlarmDataTemp.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TDaAlarmDataTemp entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除报警信息表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
